﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Garlic.CC.BaseClass;
using Garlic.CC.Utility;
using Garlic.BLL.Common;
using Garlic.DM.DataModel;
using Garlic.CC.WebUtility;
using Garlic.BLL;
using System.Collections.Generic;
using System.Text;

public partial class SystemManagement_EventList : BasePage
{
    

    #region page load
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!Page.IsPostBack)
            {
                //Init Page             
                this.EventTypeSource.SelectParameters["textWhere"].DefaultValue = String.Format("code_name = 'event_type'");
                Cache["event_type"] = CodeDetailManager.GetList("code_name = 'event_type'", "");
                Cache["event_cname"] = CodeDetailManager.GetList("code_name = 'event_cname'", "");
            }
        }
        catch (Exception ex)
        {
            this.HandleException(ex);
        }
    }
    #endregion

    #region method

    /// <summary>
    /// 初始化查询栏
    /// </summary>
    private void InitSearchBar()
    {
        //初始化查询列表

        //List<PlantInfo> plants = plantMgr.GetListByUserID();
        //List<WorkshopInfo> workshops = workshopMgr.GetListByUserID();
        //List<SupplierInfo> suppliers = SupplierManager.GetListWithWorkshop();

        //this.PartSearchBar.ListSourcXYbles = ...
    }


    void InitPartActivity()
    {
    }

    void InitPartType()
    { }


    private void Search()
    {
        StringBuilder conditionBuilder = new StringBuilder();
        if (this.ddEvent_Type.Text.Trim() != "—" && this.ddEvent_Type.Text.Trim() !="")
            conditionBuilder.AppendFormat("event_type = {0} AND ", this.ddEvent_Type.SelectedValue);

        string condition = conditionBuilder.ToString();
        if (!string.IsNullOrEmpty(condition))
        {
            if (condition.LastIndexOf(" AND ") != -1)
                condition = condition.Substring(0, condition.LastIndexOf(" AND "));
        }

        this.SmartDataViewSource.SelectParameters["textWhere"].DefaultValue = condition;

        this.SmartGrid.DataSourceID = "SmartDataViewSource";

        //this.hidden1.Value = "false";

        //string JScript = "<script language='javascript' type='text/javascript'> " +
        //        "SetSearchVisibility(0); " +
        //        " </script> ";

        //this.RegisterJS("SetDivSearchState", JScript);

    }

    private void ShowMessage(string msg)
    {
        this.Message = msg;
    }

    #endregion

    #region toolbar event handler
    /// <summary>
    /// 工具栏事件处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="args"></param>
    protected void TopToolBar_OnClick(object sender, Garlic.UI.Components.IconButtonClickEventArgs args)
    {
        switch (args.Command)
        {
            case ToolbarCommand.Search:
                Search();
                this.pnlSearch.Visible = false;
                break;
            case "Hide":
                this.pnlSearch.Visible = !this.pnlSearch.Visible;
                break;
            case ToolbarCommand.Export:
                ExportExcel();
                break;
        }

    }
    #endregion


    private void ExportExcel()
    {
        string filename = "event";
        string message;
        bool isSuccess = Garlic.Infrustructure.Utilities.WebUtil.Export(false, SmartGrid, ref filename, out message);
        if (isSuccess)
            OutputFilePath = filename;
        else
            Message = message;

    }

    protected void ddlEventType_PreRender(object sender, EventArgs e)
    {
        if (this.ddEvent_Type.Items.FindByValue("") == null)
            this.ddEvent_Type.Items.Insert(0, new ListItem("—", ""));
    }
    protected void SmartGrid_DataBound(object sender, EventArgs e)
    {
        foreach (GridViewRow row in this.SmartGrid.Rows)
        {
            
            //翻译事件类型
            List<CodeDetailInfo> CodeDetailInfo = (List<CodeDetailInfo>)Cache["event_type"];
            if (CodeDetailInfo != null)
            {
                for (int i = 0; i < CodeDetailInfo.Count; i++)
                {
                    if (CodeDetailInfo[i].Code_value == row.Cells[2].Text)
                    {
                        row.Cells[2].Text = string.Format(CodeDetailInfo[i].Value_cdescription);
                        break;
                    }
                }
            }

            //翻译事件名称
            List<CodeDetailInfo> CodeDetailEventInfo = (List<CodeDetailInfo>)Cache["event_cname"];
            if (CodeDetailEventInfo != null)
            {
                for (int i = 0; i < CodeDetailEventInfo.Count; i++)
                {
                    if (CodeDetailEventInfo[i].Value_edescription == row.Cells[1].Text)
                    {
                        row.Cells[1].Text = string.Format(CodeDetailEventInfo[i].Value_cdescription);
                        break;
                    }
                }
            }
        }
    }
    protected void SmartGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       
    }
}
